var id = GetQueryString("id");
	var vm = new Vue({
		el: '#app',
		data: {
			level:{
				logoUrl: ""
			}
		},
		methods: {
			/**
			 * 等级信息
			 */
			getInfo: function() {
				$.get(baseURL + 'userLevel/info/' + id, function(data) {
					vm.level = data.level;
					vm.$nextTick(function() {
						// 初始化图片插件
						initDropify();
					})
				}, 'JSON');
			},
			saveOrUpdate: function() {
				var url = vm.level.id == null ? "userLevel/save" : "userLevel/update";
				$.loadAjax({
					type: "POST",
					url: baseURL + url,
					data: vm.level,
					success: function(r) {
						if (r.code === 0) {
							layer.alert('提交成功', function(index) {
								backToTable();
								layer.close(index);
							});
						} else {
							layer.alert(r.msg);
						}
					}
				});
			}
		},
		mounted() {
			if (id != null && id != '') {
				this.getInfo();
			} else {
				initDropify();
			}
		}
	})
	
	function initDropify() {

		// 文件上传配置
		var drEvent = $('.dropify').dropify({
			messages: {
				'default': '点击上传照片',
				'replace': '',
				'remove': '移除',
				'error': '对不起，你上传的文件太大了',
			}
		});
		drEvent.on('dropify.error.fileSize', function(event, element) {
			layer.msg('图片大小不超过1M!');
		});
		drEvent.on('dropify.error.imageFormat', function(event, element) {
			alert('仅支持jpg,png,jpeg,gif类型图片!');
		});
		drEvent.on('dropify.afterClear', function(event, element) {
			layer.msg('图片移除成功');
		});
	}

	// 图片上传
	$('form').on("change", ".dropify", function(e) {
		
		var i = layer.load(1);
		var clazz = ('.' + $(this).attr("id")).replace('jUploadFile', '');
		$.ajaxFileUpload({
			type: "POST",
			url: baseURL + "/fileUpload",
			data: {
				'token': token
			}, //要传到后台的参数，没有可以不写  
			secureuri: false, //是否启用安全提交，默认为false  
			fileElementId: $(this).attr("id"), //文件选择框  
			dataType: 'json', //服务器返回的格式  
			async: false,
			success: function(data) {
				layer.close(i);
				if (data.code === 0) {
					layer.msg('上传成功！');
					$(clazz).val(data.url);
					vm.level.logoUrl = data.url;
				} else {
					layer.alert(data.msg);
				}
			},
			error: function(data, status, e) {
				layer.close(i);
				layer.msg('上传失败！');
			}
		});
	});

	//表单验证
	$(function() {

		$("#editForm").validate({
			rules: {
				levelName: "required",
				level: "required",
				teamNum: "required",
				taskNum: "required",
				profitRate: "required",
				profit2Rate: "required",
				shopRate: "required",
				shop1Rate: "required",
				shop2Rate: "required"
			},
			messages: {
				levelName: "等级名称不能为空",
				level: "等级不能为空",
				teamNum: "好友数不能为空",
				taskNum: "任务数不能为空",
				profitRate: "一级好友收益不能为空",
				profit2Rate: "二级好友收益不能为空",
				shopRate: "自己购物分佣",
				shop1Rate: "一级购物分佣",
				shop2Rate: "二级购物分佣"
			},
			submitHandler: function(form) { //验证通过后的执行方法

				//当前的form通过ajax方式提交
				vm.saveOrUpdate();
			},
			/* 重写错误显示消息方法,以tips方式弹出错误消息 */
			showErrors: function(errorMap, errorList) {
				var msg = "";
				$.each(errorList, function(i, v) {
					msg = (v.message + "\r\n");
					if (msg != "") layer.tips(msg, "#" + v.element.id, {
						tips: 2,
						tipsMore: true
					});
				});
			},
			/* 提示失去焦点时不验证 */
			onfocusout: false
		});
	});